package com.google.code.japndroid;

import java.util.Iterator;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;

public class PacketKillerKillAllReciever extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {

		// 稼働中のアプリの一覧を取得
		ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
		// 起動中のアプリ情報を取得
		List<RunningAppProcessInfo> runningApp = activityManager.getRunningAppProcesses();
		//List<RunningTaskInfo> runningTasks  = activityManager.getRunningTasks(1000);
		if(runningApp != null) {
			//for(RunningAppProcessInfo app : runningApp){
			Iterator<RunningAppProcessInfo> iterator=runningApp.iterator();
			while(iterator.hasNext()){
				RunningAppProcessInfo app = iterator.next();
				Log.v("PACKET KILLER","APPS " + app.processName + " IMP:" + app.importance); 
	            if (app.processName.equals("system") || app.processName.equals("com.android.phone") || 
	            		app.processName.startsWith("com.google.code.apndroid") || app.processName.startsWith("com.google.code.japndroid")) {
					Log.v("PACKET KILLER","SKIP .." + app.processName);
	                continue;
	            }
	            //if( app.importance <= 400 ){
	            //	continue;
	            //}
	            //ApplicationInfo appInfo = packageManager.getApplicationInfo(app.processName, 0);
	            activityManager.restartPackage(app.processName);
				Log.v("PACKET KILLER","KILLING .." + app.processName);
			}
			/*
			 * Iterator<RunningTaskInfo> it = runningTasks.iterator();
			 
			while(it.hasNext()){
				RunningTaskInfo t = (RunningTaskInfo)it.next();
				Log.v("PACKET KILLER","TASK " + t.topActivity.getPackageName());
			}
			*/
		}
		// 完了インテントを投げる
		Log.v("PACKET KILLER","Sending  .." +PacketKillerConstants.TASK_KILL_ALL_DONE);
        Intent msg = new Intent(PacketKillerConstants.TASK_KILL_ALL_DONE);
        context.sendBroadcast(msg);  
	}
}
